//
//  Kylin_PublishViewController.m
//  03-BuDeJie-2 TabBar设置
//
//  Created by Kylin on 16/2/20.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_PublishViewController.h"
#import "Kylin_PublishButton.h"
#import <POP.h>
#import "Kylin_PostWordViewController.h"
#import "Kylin_NavgationViewController.h"

@interface Kylin_PublishViewController()

/**slogan*/
@property(nonatomic,weak)UIImageView *sloganView;

/**<#descrption#>*/
@property(nonatomic,strong)NSArray *times;

/**按钮数组*/
@property(nonatomic,strong)NSMutableArray *buttons;

@end


@implementation Kylin_PublishViewController

-(NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    
    return _buttons;
}
- (NSArray *)times{
    
    if (!_times) {
        
        // 间隔时间
        CGFloat interval = 0.1;
        _times = @[@(5 * interval),
                   @(4 * interval),
                   @(3 * interval),
                   @(2 * interval),
                   @(0 * interval),
                   @(1 * interval),
                   @(6 * interval)];
    }
    return _times;
}

- (void)viewDidLoad{
 
    
    // 没执行完动画时不能进行交互
    self.view.userInteractionEnabled = NO;
    
    [self setupSloganView];
    
    [self setupButtons];
}


- (void)setupButtons{
    // 数据
    NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];
    
    // 按钮的个数
    NSUInteger count = images.count;
    int maxColsCount = 3; // 一共多少行
    NSUInteger rowsCount = (count + maxColsCount - 1) / maxColsCount; // 总行数
    
    // 按钮尺寸
    CGFloat buttonW = Kylin_ScreenW / maxColsCount;
    CGFloat buttonH = buttonW * 0.9;
    
    // 计算按钮的中心位置
    CGFloat buttonStartY = (Kylin_ScreenH - rowsCount * buttonH) * 0.5;
    for (int i = 0; i < count; i ++) {
        Kylin_PublishButton *button = [Kylin_PublishButton buttonWithType:UIButtonTypeCustom];
        
        // 按钮的尺寸为0，还是能看见文字缩成一个点，设置按钮的尺寸为负数，那么就看不见文字了

        button.width = -1;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        
        // 按钮的X,Y
        CGFloat buttonX = (i % maxColsCount ) * buttonW;
        CGFloat buttonY = buttonStartY + (i / maxColsCount) * buttonH;
    
        [self.view addSubview:button];
        [self.buttons addObject:button];
        
        // 动画 表示在view 上面做动画
        POPSpringAnimation *popAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        
        popAnim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY - Kylin_ScreenH, buttonW, buttonH)];
        popAnim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        
        
        // 弹簧效果控制
        popAnim.springSpeed = 10;
        popAnim.springBounciness = 10;
        
        // 计算动画执行时间  获取当前时间 加上第一个按钮动画执行时间
        popAnim.beginTime = CACurrentMediaTime() + [self.times[i] doubleValue];
        
        [button pop_addAnimation:popAnim forKey:nil];
        
    }
}
- (void)setupSloganView{

   
    
    CGFloat sloganY = Kylin_ScreenH * 0.2;
   
    UIImageView *sloganView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
         sloganView.y = sloganY -  Kylin_ScreenH ;
   
    sloganView.centerX = Kylin_ScreenW * 0.5;
    
    [self.view addSubview:sloganView];
    self.sloganView = sloganView;
    
    // 动画 表示在view 上面做动画
    POPSpringAnimation *popAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    
    // 把一个数字包装成一们对象
    popAnim.toValue = @(sloganY);
   
    popAnim.springSpeed = 10;
    popAnim.springBounciness = 10;
    
    // 计算动画执行时间  获取当前时间 加上第一个按钮动画执行时间
    popAnim.beginTime = CACurrentMediaTime() + [[self.times lastObject] doubleValue];
    
    Kylin_WeakSelf
    [popAnim setCompletionBlock:^(POPAnimation *popAnim, BOOL finished) {
        weakSelf.view.userInteractionEnabled = YES;
    }];
    
//    popAnim.completionBlock = ^(POPAnimation *p, BOOL f){
//         weakSelf.view.userInteractionEnabled = YES;
//    };
//  
    [sloganView.layer pop_addAnimation:popAnim forKey:nil];
    
    
}

- (void)buttonClick:(Kylin_PublishButton *)button{
    
    Kylin_LogFunc;
    
    NSInteger index = [self.buttons indexOfObject:button];
    [self exit:^{
        switch (index) {
            case 2:{
            
                Kylin_PostWordViewController *postWord = [[Kylin_PostWordViewController alloc]init];
                [self.view.window.rootViewController presentViewController:[[Kylin_NavgationViewController alloc]initWithRootViewController:postWord] animated:YES completion:nil];
                
                break;
            }
                
            default:
                break;
        }
    }];
}


- (void)exit:(void(^)())task{
    
    // 禁止交互
    self.view.userInteractionEnabled = NO;
    
    
    // 让按钮执行动画
    for (int i = 0; i < self.buttons.count; i ++) {
        
        Kylin_PublishButton *button = self.buttons[i];
        // 动画 表示在view 上面做动画
        POPBasicAnimation *popAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        popAnim.toValue = @(button.layer.position.y + Kylin_ScreenH);
        popAnim.beginTime = CACurrentMediaTime() + [self.times[i] doubleValue];
        
        [button.layer pop_addAnimation:popAnim forKey:nil];
        
    }
    
    Kylin_WeakSelf
    // 动画 表示在view 上面做动画
    POPBasicAnimation *Anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    
    Anim.toValue = @(self.sloganView.layer.position.y + Kylin_ScreenH);
    
    Anim.beginTime = CACurrentMediaTime() + [self.times.lastObject doubleValue];
    
    [Anim setCompletionBlock:^(POPAnimation *Anim, BOOL finished) {
        
        [weakSelf dismissViewControllerAnimated:NO completion:nil];
        
        if (task) {
            task();
        }
    }];
    
    
    [self.sloganView.layer pop_addAnimation:Anim forKey:nil];
    
}
- (IBAction)cancelButtonClick {
   
    [self exit:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self exit:nil];
}
@end
